package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    @Override
    public void createBankAccount(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        Long count = lambdaQuery().eq(BankAccount::getBankName, bankAccountUpsertReqDTO.getBankName())
                .eq(BankAccount::getAccount, bankAccountUpsertReqDTO.getAccount())
                .count();
        if (count > 0) {
            throw new RuntimeException("该账户已存在");
        }
        BankAccount bankAccount = BeanUtils.copyBean(bankAccountUpsertReqDTO, BankAccount.class);
        bankAccount.setId(UserContext.currentUserId());
        bankAccount.setCreateTime(LocalDateTime.now());
        bankAccount.setUpdateTime(LocalDateTime.now());
        this.save(bankAccount);
    }

    @Override
    public BankAccountResDTO findCurrentUserBankAccount() {
        Long userId = UserContext.currentUserId();
        BankAccount bankAccount = lambdaQuery().eq(BankAccount::getId, userId).one();
        if (bankAccount == null) {
            return null;
        }
        return BeanUtils.copyBean(bankAccount, BankAccountResDTO.class);
    }
}
